package cn.store.service.serviceImp;

import cn.store.dao.ProductDao;
import cn.store.domain.PageModel;
import cn.store.domain.Product;
import cn.store.service.ProductService;
import cn.store.utils.BeanFactory;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class ProductServiceImp implements ProductService {


	ProductDao ProductDao=(ProductDao) BeanFactory.createObject("ProductDao");

	@Override
	public void saveProduct(Product product) throws Exception {
		ProductDao.saveProduct(product);
		
	}

	@Override
	public void updateProduct(Product product) throws SQLException {
		ProductDao.updateProduct(product);
	}

	@Override
	public int deleteProduct(String pid) throws SQLException {
		return ProductDao.deleteProduct(pid);
	}

	
	@Override
	public Product findProductByPid(String pid) throws Exception {
		return ProductDao.findProductByPid(pid);
		
	}

	//返回最热商品
	@Override
	public List<Product> findHots() throws Exception {
		return ProductDao.findHots();
	}

	//返回最新商品
	@Override
	public List<Product> findNews() throws Exception {
		return ProductDao.findNews();
	}

	@Override
	public PageModel findProductsByCidWithPage(String cid, int curNum) throws Exception {
		//1_创建PageModel对象 目的:计算分页参数
		//统计当前分类下商品个数  select count(*) from product where cid=?
		int totalRecords=ProductDao.findTotalRecords(cid);
		PageModel pm=new PageModel(curNum,totalRecords,24);
		//2_关联集合 select * from product where cid =? limit ? ,?
		List list=ProductDao.findProductsByCidWithPage(cid,pm.getStartIndex(),pm.getPageSize());
		pm.setList(list);
		//3_关联url
		pm.setUrl("ProductServlet?method=findProductsByCidWithPage&cid="+cid);
		return pm;
	}

	@Override
	public PageModel findAllProductsWithPage(int curNum, Map<String,Object> searchWords) throws Exception {
		//1_创建对象
		int totalRecords=ProductDao.findTotalRecords(searchWords);
		PageModel pm=new PageModel(curNum,totalRecords,24);
		//2_关联集合 select * from product limit ? , ?
		List<Product> list=ProductDao.findAllProductsWithPage(pm.getStartIndex(),pm.getPageSize(),searchWords);
		pm.setList(list);
		//3_关联url
		pm.setUrl("AdminProductServlet?method=findAllProductsWithPage");
		return pm;
	}

}
